<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php
/**
 * Content of m_Ranger CMS.
 * @license MIT License
 * @author Nightlolo
 * @since 1.0
 */
class Accounts extends CI_Controller{

    public function  __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->database('accounts');
    }

    public function index(){
        if(!$this->session->userdata('u.id'))
            return $this->login();
        else
            return $this->profile();
    }

    /**
     * Action de connexion. La connexion peut être faite par GET ou POST.
     * Renvoie à la vue account_login.
     * @param string $ndc
     * @param string $mdp
     */
    public function login($ndc = null, $mdp = null){
        if(!isset($ndc, $mdp)){
            $ndc = $this->input->post('account', TRUE);
            $mdp = $this->input->post('password', TRUE);
        }
        $found = $this->db->select('id, gmlevel')
                ->from('player_accounts')
                ->where('username', $ndc)
                ->where('password', $mdp)
                ->get()
                ->row();
        if(empty($found)){
           $view = $this->load->view('accounts_login',
           array(
               'data' => 'Les données de connexions que vous avez entré sont érronés.',
               'fail' => TRUE
               ),
            TRUE);
        }
        elseif(!empty($found)){
           // Set the users id and gmlevel.
           $this->session->set_userdata(array(
               'u.id' => (int)$found->id,
               'u.gmlevel' => (int)$found->gmlevel
           ));
           // Load the success view
           $view = $this->load->view('accounts_login',
           array(
               'data' => 'Vous êtes connectés avec succès.',
               'fail' => FALSE
               ),
            TRUE);
           // Load final view :)
           $this->load->view('_layout', array('content' => $view));
        }
    }

    public function logout(){

    }

    public function register(){
        
    }

    public function profile(){
        
    }
}
?>
